package com.spring.springboot.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
*@author LSY
*Time: 2018年12月7日下午5:51:24
*
*
*/
public class SessionInterceptor implements HandlerInterceptor{

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
		System.out.println(request.getRequestURI());
		if(request.getRequestURI().equals("/user/login") || request.getRequestURI().equals("/user/login_view") || request.getRequestURI().indexOf("jpg") != -1 || request.getRequestURI().indexOf("servlet") != -1) {
			return true;
		}
		Object obj = request.getSession().getAttribute("user");
		if(obj == null) {
			response.sendRedirect("/user/login_view");
			return false;
		}
		return true;
	}
	@Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
